const e = require('express');
const express = require('express');
const pool = require('../../pool.js');
const customer = express.Router();
// 用户信息查询
customer.get('/cusfind', (req, res) => {
    let u_true_name = req.query.uname;
    pool.query('select * from customer where u_true_name = ?', [u_true_name], (err, r) => {
        if (err) {
            throw err;
        }
        res.send(r);
    })
})

// 地址查询
customer.post('/addr', (req, res) => {
    let addr = '%' + req.body.state + '%';
    let page = parseInt(req.body.page) * 20;
    pool.query('select  CityShortName,Name  from areas where CityShortName like ? limit ?,20', [addr, page], (err, r) => {
        if (err) {
            throw err;
        }
        res.send(r)
    })
})

// 用户登录
customer.post('/cuslogin', (req, res) => {
    let email = req.body.email;
    if (req.body.uname != '') {
        let uname = req.body.uname;
        let upwd = req.body.upwd;
        pool.query('select * from customer where uname= ? and upwd=?', [uname, upwd], (err, r) => {
            if (err) {
                throw err;
            }
            res.send(r);
        })
    } else {
        let uname = Math.floor(Math.random() * 10000000000);
        let upwd = Math.floor(Math.random() * 10000000000);
        let user_name = '用户' + Math.floor(Math.random() * 10000000);
        pool.query('insert customer set uemail=?,uname=?,upwd=?,u_true_name=?', [email, uname, upwd, user_name], (err, r) => {
            if (err) {
                pool.query('select u_true_name from customer where uemail=?', [email], (error, data) => {
                    if (error) {
                        throw err;
                    }
                    res.send(data);
                })
            } else {
                res.send([{ u_true_name: user_name }]);
            }
        })
    }
})

// 查询用户评论
customer.get('/comment', (req, res) => {
    let user = req.query.user;
    pool.query('select * from comments where user = ?', [user], (err, r) => {
        if (err) {
            throw err;
        }
        res.send(r);
    })
});
customer.post('/reply', (req, res) => {
    let user = req.body.user;
    let reply = req.body.reply;
    pool.query('update comments set shopReply=? where user =?', [reply, user], (err, r) => {
        if (err) {
            throw err;
        }
        res.send(r);
    })
})
module.exports = customer;